<?php

class Controller_Manager_Banner extends Controller
{
    public function __construct()
    {
        $aUser = getCurrentUser();
		if (!isset($aUser['utype']) || $aUser['utype'] != 'manager')
		{
		    Controller_Error::forbidden();
		}
    }
    
	public function edit()
	{
	    $id = isset($this->params[0]) ? intval($this->params[0]) : 0;
	    $banner = new Model_Banner($id);
	    $media = new Model_Media();
	    $aData = $banner->values;
        $aMedia = array();
	    if ($id)
        {
            $aMedia = $media->findBy('banner_id', $id);
            foreach ($aMedia as $k => $v)
            {
                $aMedia[$k] = $v->values;
            }
        }
	    if ($aData['start_date'])
	    {
	        $aData['start_date'] = date('d.m.Y H:i', $aData['start_date']);
	    }
	    if ($aData['end_date'])
	    {
	        $aData['end_date'] = date('d.m.Y H:i', $aData['end_date']);
	    }
	    
	    $errors = array();
	    if (Request::isPost())
	    {
	        $aData = Request::postVars(array(
                'title', 'url', 'width', 'width_in_percents',
                'height', 'height_in_percents', 'code_block', 'start_date',
                'end_date', 'clicks_limit', 'views_limit',
                'daily_clicks_limit', 'daily_views_limit',
            ));
	        $validator = new Validator();
    		$validator->add(new Rule_NotEmpty('title', 'Введите название'));
    		$validator->add(new Rule_NotEmpty('url', 'Введите URL'));
    		$validator->add(new Rule_IntegerPositive('width', 'Введите ширину'));
    		$validator->add(new Rule_IntegerPositive('height', 'Введите высоту'));
    		$validator->add(new Rule_DatePeriod('start_date', 'end_date', 'Введите дату начала'));
    		$validator->add(new Rule_NotEmpty('end_date', 'Введите дату окончания'));
            $validator->add(new Rule_Number('clicks_limit', 'Корректно заполните поле', 0));
            $validator->add(new Rule_Number('views_limit', 'Корректно заполните поле', 0));
            $validator->add(new Rule_Number('daily_clicks_limit', 'Корректно заполните поле', 0));
            $validator->add(new Rule_Number('daily_views_limit', 'Корректно заполните поле', 0));

	        if ($validator->validate($aData))
    		{
    		    $aUser = getCurrentUser();
    		    $aData['creator_id'] = $aUser['id'];
    		    $aData['start_date'] = strtotime($aData['start_date']);
    		    $aData['end_date']   = strtotime($aData['end_date']);

		        if ($id)
		        {
		            $banner->save($aData);
		            $media->editMassMedia($banner->id);
                    Redirect('/manager/banner/edit/'.$banner->id);
		        }
		        else
		        {
                    $aData['cdate'] = time();
		            $banner->values = $aData;
    		        $banner->save();
    		        if ($banner->id)
    		        {
    		            $media->addMassMedia($banner->id);
    		            Redirect('/manager/banner/edit/'.$banner->id);
    		        }
		        }
    		}
    		else
    		{
    		    $errors = $validator->errors;
    		}
	    }
	    
	    $aClients = DB::hashedRows('SELECT * FROM accounts WHERE utype = "client"');
	    
		$this->view->add(array(
		    'id'       => $id,
		    'aData'    => $aData,
		    'aClients' => $aClients,
		    'aMedia'   => $aMedia,
		    'errors'   => $errors,
		));
		return $this->view->render('manager/banner/edit.tpl');
	}
}